package com.gk.study.controller;

import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.gk.study.common.APIResponse;
import com.gk.study.common.ResponeCode;
import com.gk.study.common.Result;
import com.gk.study.entity.Guest;
import com.gk.study.entity.Home;
import com.gk.study.service.HomeService;
import com.gk.study.utils.AliyunOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/thing")
@Slf4j
@CrossOrigin(origins = {"*"})
public class HomeController {

    @Autowired
    private HomeService homeService;

    /**
     * 查询房间信息
     *
     * @param keyword
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public APIResponse list(@RequestParam(required = false)String keyword, @RequestParam(required = false)String sort, @RequestParam(required = false)String c, @RequestParam(required = false)String tag){
        List<Home> list =  homeService.getHomeList(keyword, sort, c, tag);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }
    @PostMapping("/create")
    public APIResponse<Home> create(Home home){
        boolean save = homeService.save(home);
        return new APIResponse(ResponeCode.SUCCESS, "添加成功", save);
    }
    @PostMapping("/upload")
    public APIResponse<String> upload(MultipartFile file)throws Exception{
        String originalFilename = file.getOriginalFilename();
        //保证名字唯一性,防止覆盖,使用UUID
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = AliyunOssUtil.upLoad(filename, file.getInputStream());
        return new APIResponse(ResponeCode.SUCCESS, "添加成功", url);
    }
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public APIResponse detail(String id){
        Home home =  homeService.getHomeById(id);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", home);
    }

}
